package com.zhinong.controller.admin;

import com.zhinong.annotation.SelectCache;
import com.zhinong.dto.ExpertPageQueryDTO;
import com.zhinong.entity.Expert;
import com.zhinong.result.PageResult;
import com.zhinong.result.Result;
import com.zhinong.service.ExpertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "专家相关接口")
@Slf4j
@RequestMapping("/admin/expert")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    @Autowired
    private RedisTemplate redisTemplate;


    @SelectCache(name = "expertPageQuery")
    @GetMapping("/page")
    @ApiOperation("专家分页查询")
    public Result<PageResult> pageQuery(ExpertPageQueryDTO expertPageQueryDTO){
        log.info("专家分页查询,参数为:{}",expertPageQueryDTO);
        PageResult pageResult = expertService.pageQuery(expertPageQueryDTO);
        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询专家")
    public Result<Expert> getByID(@PathVariable Long id){
        log.info("根据id查询专家,参数为:{}",id);
        Expert expert = expertService.getByID(id);
        return Result.success(expert);
    }

    @PostMapping
    @ApiOperation("新增专家")
    public Result save(@RequestBody Expert expert){
        log.info("新增专家：{}",expert);
        expertService.save(expert);
        return Result.success();
    }

    @PutMapping("/{id}")
    @ApiOperation("编辑专家信息")
    public synchronized Result update(@RequestBody Expert expert){
        log.info("编辑专家信息：{}",expert);
        expertService.update(expert);
        return Result.success(expert);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除专家信息")
    public Result delete(@PathVariable Long id){
        log.info("删除专家：{}",id);
        expertService.delete(id);
        return Result.success();
    }
}
